/*
 *@author: YangJie
 *@date: 2021-04-16 16:13:18
 *@version:
 *@description:参数设置->设备参数设置->下拉参数
*/
import React, { Component } from 'react';
import { Button, Form, Input, Tabs } from 'antd';
import '@/assets/css/settings';
import { euqApplicationName } from '../../../config'
const { TabPane } = Tabs;
const FormItem = Form.Item;
const { TextArea } = Input;
class App extends Component {
	state = {
		loading: false, //加载中...
		dataSource: [], //数据源
		total: 0,
		formValues: {},
		cityData: [],
		mainFactoryList: [],
		listBasicData: [	//基本数据保存
			{ type: 1, data: [], str: '' },
			{ type: 2, data: [], str: '' },
			{ type: 3, data: [], str: '' },
			{ type: 4, data: [], str: '' },
			{ type: 5, data: [], str: '' },
			{ type: 6, data: [], str: '' },
			{ type: 7, data: [], str: '' },
			{ type: 8, data: [], str: '' },
			{ type: 9, data: [], str: '' },
			{ type: 10, data: [], str: '' },
			{ type: 11, data: [], str: '' },
			{ type: 12, data: [], str: '' },
			{ type: 13, data: [], str: '' },
			{ type: 14, data: [], str: '' },
			{ type: 15, data: [], str: '' },
			{ type: 16, data: [], str: '127.119.1.1:8080' },
			{ type: 17, data: [], str: '' },
		],
	};
	componentDidMount() {
		this.fetch()
	}
	setListBasicData(data) {
		let listBasicData = this.state.listBasicData
		let basicData = data
		for (let i in basicData) {
			for (let b in listBasicData) {
				if (basicData[i].type == listBasicData[b].type) {
					listBasicData[b].data.push(basicData[i])
					listBasicData[b].str += basicData[i].configValue.split(/[(\r\n)\r\n]+/) + '\n'
				}
			}
		}
		this.setState({ listBasicData })
	}


	//初始化加载数据
	fetch = () => {
		for (let i = 0; i < 17; i++) {
			this.get(`${euqApplicationName}/api/findEquipmentNextParameterPageList?typeId=${i + 1}`).then(res => {
				if (res.code == "200") {
					this.setListBasicData(res.data);
				}
			});
		}
	};
	getInfo = () => {
		this.post('/carrierPrice/mainFactoryList').then(res => this.setState({ mainFactoryList: res.data })); //主机厂
	};

	handleConfirm = (value, api, chooseData) => {
		if (api === 1) {
			this.get(`/airCargo/delAirCargo?id=${chooseData.id}`).then(res => {
				this.success();
				this.fetch({ ...this.state.formValues });
			});
		}
	};

	// 提交modal
	handleSubmit = data => {
		const id = this.state.chooseData && this.state.chooseData.id;
		if (id) {
			data = Object.assign({}, data, { id: id });
			data.updateId = localStorage.getItem('userId');
		} else {
			data.createId = localStorage.getItem('userId');
		}
		this.post('/airCargo/addAirCargo', { data }).then(res => {
			this.success();
			this.fetch({ ...this.state.formValues });
			this.onClose();
		});
	};

	// 打开modal，判断是修改还是新增 0新增 1修改
	handleModal = (type, data) => {
		if (type) {
			this.setState({
				drawerTitle: '修改',
				drawerVisible: true,
				chooseData: data
			});
		} else {
			this.setState({
				drawerTitle: '新增',
				drawerVisible: true
			});
		}
	};

	onClose = () => {
		this.setState({
			drawerVisible: false,
			chooseData: {}
		});
		this.modalRef.props.form.resetFields();
	};

	queryRepairList(type) {
		if (type == 1) {
			this.props.form.validateFields(['dropDownId_1'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_1, 1)
				}
			})
		} else if (type == 2) {
			this.props.form.validateFields(['dropDownId_2'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_2, 2)
				}
			})
		} else if (type == 3) {
			this.props.form.validateFields(['dropDownId_3'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_3, 3)
				}
			})
		} else if (type == 4) {
			this.props.form.validateFields(['dropDownId_4'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_4, 4)
				}
			})
		} else if (type == 5) {
			this.props.form.validateFields(['dropDownId_5'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_5, 5)
				}
			})
		} else if (type == 6) {
			this.props.form.validateFields(['dropDownId_6'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_6, 6)
				}
			})
		} else if (type == 7) {
			this.props.form.validateFields(['dropDownId_7'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_7, 7)
				}
			})
		} else if (type == 8) {
			this.props.form.validateFields(['dropDownId_8'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_8, 8)
				}
			})
		} else if (type == 9) {
			this.props.form.validateFields(['dropDownId_9'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_9, 9)
				}
			})
		} else if (type == 10) {
			this.props.form.validateFields(['dropDownId_10'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_10, 10)
				}
			})
		} else if (type == 11) {
			this.props.form.validateFields(['dropDownId_11'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_11, 11)
				}
			})
		} else if (type == 12) {
			this.props.form.validateFields(['dropDownId_12'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_12, 12)
				}
			})
		} else if (type == 13) {
			this.props.form.validateFields(['dropDownId_13'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_13, 13)
				}
			})
		} else if (type == 14) {
			this.props.form.validateFields(['dropDownId_14'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_14, 14)
				}
			})
		} else if (type == 15) {
			this.props.form.validateFields(['dropDownId_15'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_15, 15)
				}
			})
		} else if (type == 16) {

		} else if (type == 17) {
			this.props.form.validateFields(['dropDownId_17'], (err, values) => {
				if (!err) {
					this.queryRepairListPost(values.dropDownId_17, 17)
				}
			})
		}
	}
	queryRepairListPost(values, focus) {		//接口保存数据
		let snsArr = values.split(/[(\r\n)\r\n]+/);
		let listBasicData = this.state.listBasicData[focus - 1].data			//接口保存前数据
		let snsNeed = [];		//此时输入框行数
		let options = [];
		snsArr.forEach((item, index) => {
			if (item.replace(/[ ]/g, "").length == 0 || !item) {
			} else {
				snsNeed.push(item.trim())
			}
		})
		let xiugaiArr = [], addArr = [], delArr = [];
		if (snsNeed.length == listBasicData.length) {
			for (let i in listBasicData) {
				if (listBasicData[i].configValue != snsNeed[i]) {
					listBasicData[i].configValue = snsNeed[i]
					xiugaiArr.push(listBasicData[i])
				}
			}
		} else if (snsNeed.length > listBasicData.length) {
			for (let i in snsNeed) {
				if (listBasicData[i]) {
					if (listBasicData[i].configValue != snsNeed[i]) {
						listBasicData[i].configValue = snsNeed[i]
						xiugaiArr.push(listBasicData[i])
					}
				} else {
					addArr.push({
						configValue: snsNeed[i],
						createrId: 0,
						id: 0,
						sort: "string",
						type: focus
					})
				}
			}
		} else if (snsNeed.length < listBasicData.length) {
			for (let i in listBasicData) {
				if (snsNeed[i]) {
					if (listBasicData[i].configValue != snsNeed[i]) {
						listBasicData[i].configValue = snsNeed[i]
						xiugaiArr.push(listBasicData[i])
					}
				} else {
					delArr.push(listBasicData[i])
				}
			}
		}
		if (xiugaiArr.length > 0) {
			for (let i in xiugaiArr) {
				let obj = {
					data: xiugaiArr[i]
				}
				this.post(`${euqApplicationName}/api/editEquipmentNextParameter`, obj).then(res => {
					if (res.code == "200") {
						this.success()
						this.fetch()
						console.log(res)
					}
				});
			}
		}
		if (addArr.length > 0) {
			let obj = {
				data: addArr
			}
			this.post(`${euqApplicationName}/api/addEquipmentNextParameterList`, obj).then(res => {
				if (res.code == "200") {
					this.success()
					this.fetch()
					console.log(res)
				}
			});
		}
		if (delArr.length > 0) {
			let str = ''
			for (let i in delArr) {
				str += delArr[i].id + ","
			}
			this.get(`${euqApplicationName}/api/deleteEquipmentNextParameter?ids=` + str.slice(0, str.length - 1)).then(res => {
				if (res.code == "200") {
					this.success()
					this.fetch()
					console.log(res)
				}
			});
		}
	}

	render() {
		const { chooseData, drawerVisible, drawerTitle, dataSource, loading, total, formValues } = this.state;

		const { getFieldDecorator } = this.props.form;
		const formItemLayout = {
			labelCol: {
				xs: { span: 24 },
				sm: { span: 6 }
			},
			wrapperCol: {
				xs: { span: 24 },
				sm: { span: 18 }
			}
		};
		return (
			<div className="shadow-radius">
				<Tabs>
					<TabPane tab="计量单位" key="1">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>计量单位列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_1', { initialValue: this.state.listBasicData[0].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(1)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="使用状况" key="2">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>使用状况列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_2', { initialValue: this.state.listBasicData[1].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(2)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="故障等级" key="3">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>故障等级列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_3', { initialValue: this.state.listBasicData[2].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(3)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="故障原因" key="4">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>故障原因列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_4', { initialValue: this.state.listBasicData[3].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(4)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="维修级别" key="5">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>维修级别列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_5', { initialValue: this.state.listBasicData[4].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(5)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="保养等级" key="6">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>保养等级列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_6', { initialValue: this.state.listBasicData[5].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(6)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="维修类别" key="7">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>维修类别列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_7', { initialValue: this.state.listBasicData[6].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(7)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="紧急程度" key="8">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>紧急程度列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_8', { initialValue: this.state.listBasicData[7].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(8)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="设备标记" key="9">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>设备标记列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_9', { initialValue: this.state.listBasicData[8].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(9)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="维修评价" key="10">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '50%' }}>
							<span>维修评价列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_10', { initialValue: this.state.listBasicData[9].str })(<TextArea style={{ width: 500, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(10)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					{/* <TabPane tab="维修中状态" key="11">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>维修中状态列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_11', { initialValue: this.state.listBasicData[10].str })(<TextArea style={{ width: 300, height: 400 }} disabled={true}/>)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(11)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane> */}
					<TabPane tab="外委中状态" key="12">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>外委中状态列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_12', { initialValue: this.state.listBasicData[11].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(12)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="检验类型" key="13">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>检验类型列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_13', { initialValue: this.state.listBasicData[12].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(13)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="巡检跳过原因" key="14">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>巡检跳过原因列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_14', { initialValue: this.state.listBasicData[13].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(14)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="点检跳过原因" key="15">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>点检跳过原因列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_15', { initialValue: this.state.listBasicData[14].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(15)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="流程类型" key="17">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '30%' }}>
							<span>流程类型列表:(每行即为一个参数值)</span>
							<Form.Item>{getFieldDecorator('dropDownId_17', { initialValue: this.state.listBasicData[16].str })(<TextArea style={{ width: 300, height: 400 }} />)}</Form.Item>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(17)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
					<TabPane tab="打印服务器地址" key="16">
						<Form {...formItemLayout} layout="horizontal" style={{ width: '40%' }}>
							<FormItem>
								<label>打印服务器地址：</label>
								<Input placeholder={this.state.listBasicData[15].str} allowClear lab></Input>
								<p>这里设置打印服务器地址.例:127.0.0.1:9000</p>
								<p>打印服务器:安装了打印客户端的电脑</p>
								<a>点击下载打印客户端</a>
							</FormItem>

							<FormItem>
								<Button type="primary" htmlType="submit" className={'btn'} onClick={() => this.queryRepairList(16)}>
									保存
								</Button>
							</FormItem>
						</Form>
					</TabPane>
				</Tabs>
			</div>
		);
	}
}
export default Form.create()(App);
